#ifndef ENDPOINT_ADDR_H
#define ENDPOINT_ADDR_H

#include <inttypes.h>
#include <netinet/in.h>
#include <string.h>

namespace usnet {

enum class InetProto : uint8_t {
    IPv4 = 0x40,
    IPv6 = 0x60,
};

struct EndpointAddr {
    EndpointAddr(const struct sockaddr_in&  addr):
        inet_proto_ { InetProto::IPv4 }
    {
        be_port_ = addr.sin_port;
        be_ip_.v4_ = addr.sin_addr.s_addr;
    }

    EndpointAddr(const struct sockaddr_in6& addr):
        inet_proto_ { InetProto::IPv6 }
    {
        be_port_ = addr.sin6_port;
        memcpy(be_ip_.v6_, &addr.sin6_addr, sizeof(be_ip_.v4_));
    }

    EndpointAddr(const EndpointAddr& addr):
        inet_proto_ { addr.inet_proto_ },
        be_port_ { addr.be_port_ }
    {
        memcpy(&be_ip_, &addr.be_ip_, sizeof(be_ip_));
        memcpy(eth_addr_, addr.eth_addr_, sizeof(eth_addr_));
    }
    
    InetProto inet_proto_;
    uint16_t be_port_ { };
    union {
        uint32_t v4_;
        uint8_t  v6_[16];
    } be_ip_ { };
    uint8_t eth_addr_[6] { };
};
} // end of namespace usnet
#endif